﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Kugar.UI.MVC2
{
    public  static class PagerExtMethod
    {
        public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int recordCount)
        {
            return Pager(html, currentPageStr, pageSize, recordCount, 3);
        }

        /// <summary>
        ///     分页Pager显示
        ///     包含的样式为: p_up: 上一页
        ///                          p_down:下一页样式
        ///                          p_current:当前页
        ///                          p_home:首页
        ///                          p_last:末页样式
        ///                          p_disable:被禁用的按钮样式
        ///                          p_num:普通页码的样式
        ///                          p_count: "第{0}页/共{1}页"的样式
        ///     生成的连接样式为: http://xx.com/xxx?{currentPageStr}=PageIndex
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr">标识当前页码的QueryStringKey</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="recordCount">总数据量</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int recordCount, int pageDisplayPerSide = 3)
        {
            if (recordCount <= 0 || pageSize <= 0)
            {
                return "";
            }

            var queryString = html.ViewContext.HttpContext.Request.QueryString;

            int currentPage = 1; //当前页

            if (!int.TryParse(queryString[currentPageStr], out currentPage)) currentPage = 1; //与相应的QueryString绑定

            var totalPages = Math.Max((recordCount + pageSize - 1) / pageSize, 1); //总页数

            var dict = new RouteValueDictionary(html.ViewContext.RouteData.Values);

            var output = new StringBuilder(150);

            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                    dict[key] = queryString[key];

            if (totalPages > 1)
            {
                if (currentPage != 1)
                {//处理首页连接
                    dict[currentPageStr] = 1;
                    output.AppendFormat("<span class=\"p_home\">{0}</span>", html.RouteLink("首页", dict));
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    dict[currentPageStr] = currentPage - 1;
                    output.AppendFormat("<span class=\"p_up\">{0}</span>", html.RouteLink("上一页", dict));
                }
                else
                {
                    output.AppendFormat("<span class=\"p_disable\">{0}</span>", "");
                }

                for (int i = (pageDisplayPerSide - 1) * -1; i < pageDisplayPerSide; i++)
                {
                    var tempPageStr = (currentPage + i).ToString();
                    if (tempPageStr.Length > 2 && i != 0)
                    {
                        tempPageStr = tempPageStr.Substring(0, 2) + "..";
                    }

                    if (i == 0)
                    {
                        output.Append(string.Format("<span title=\"{1}\" class=\"p_current\">{0}</span>", tempPageStr, currentPage));
                    }
                    else if ((currentPage + i) >= 1 && (currentPage + i) <= totalPages)
                    {
                        dict[currentPageStr] = currentPage + i;
                        output.AppendFormat("<span title=\"{1}\" class=\"p_num\">{0}</span>", html.RouteLink(tempPageStr, dict), currentPage + i);
                    }
                }

                //for (int i = 0; i <= 8; i++)
                //{//一共最多显示10个页码，前面5个，后面5个
                //    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                //        if (currint == i)
                //        {//当前页处理
                //            output.Append(string.Format("<span class=\"p_current\">{0}</span>", currentPage));
                //        }
                //        else
                //        {//一般页处理
                //            dict[currentPageStr] = currentPage + i - currint;
                //            output.AppendFormat("<span class=\"p_num\">{0}</span>", html.RouteLink((currentPage + i - currint).ToString(), dict));
                //        }
                //}
                if (currentPage < totalPages)
                {//处理下一页的链接
                    dict[currentPageStr] = currentPage + 1;
                    output.AppendFormat("<span class=\"p_down\">{0}</span>", html.RouteLink("下一页", dict));
                }
                else
                {
                    output.AppendFormat("<span class=\"p_disable\">{0}</span>", "下一页");
                }
                if (currentPage != totalPages)
                {
                    dict[currentPageStr] = totalPages;
                    output.AppendFormat("<span class=\"p_last\">{0}</span>", html.RouteLink("末页", dict));
                }
            }
            output.AppendFormat("<span class=\"p_count\">{0}/{1},共{2}记录</span>", currentPage, totalPages, recordCount);//这个统计加不加都行
            return output.ToString();
        }
    }
}
